﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using NUnit.Framework;
using MongoDB.Bson.Serialization;
using Charisma.MongoDB.Tests.SerializationModel;
using FluentAssertions;

namespace Charisma.MongoDB.Tests
{
    [TestFixture]
    public class SerializationTests
    {
        private static DbModel GetDbModel()
        {
            var c = new Context();
            return c.DbModel;
        }


        [Test]
        public void SerializerAndClassMap_NotNull()
        {
            var dbm = GetDbModel();
            foreach (var tc in dbm.Entities) {
                BsonSerializer.LookupSerializer(tc.Type).Should().NotBeNull();
                BsonClassMap.IsClassMapRegistered(tc.Type).Should().Be(true);
            }
        }

        [Test]
        public void MemberIsIgnored_MemberMapIsAbsent()
        {
            var dbm = GetDbModel();
            foreach (var tc in dbm.Entities) {
                var m = BsonClassMap.LookupClassMap(tc.Type);
                foreach (var fc in tc.Members.Where(f => f.IsIgnored))
                    m.MemberMaps.Where(mm => mm.ElementName == fc.Name).Should().BeEmpty();
            }
        }

        [Test]
        public void MemberHasName_MemberMapElementHasName()
        {
            var dbm = GetDbModel();
            foreach (var tc in dbm.Entities) {
                var m = BsonClassMap.LookupClassMap(tc.Type);
                foreach (var fc in tc.Members.Where(f => !f.IsIgnored)) {
                    var name = string.IsNullOrEmpty(fc.Alias) ? fc.Name : fc.Alias;
                    m.MemberMaps.Where(mm => mm.ElementName == name).Should().NotBeEmpty();
                }
            }
        }
    }
}
